using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Framework.Library.Base.Base;
using Framework.Library.Base.DataAccess;
using Framework.Library.Core.Utils;
using Framework.Library.Data.DAL;
using Framework.Library.Data.DAO;
using System.IO;
using Framework.Library.Data.DAL.JObject;

namespace Framework.Portal.Layouts.Admin.Components.Portals
{
    public partial class PortalManagement : BaseControl
    {
        private bool Edit;
        public string MENUNAME = string.Empty;

        public PortalManagement()
        {
        }

        public PortalManagement(JSection section)
        {
            MENUNAME = section.title;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadPortal();
                List<PortalLanguage> dtLanguages = ManagerFactory.DAOEntity.GetList<PortalLanguage>(new PortalLanguage());
                PortalAccess.LoadLanguages(dtLanguages, General_drgLanguage.Items);                

                int srtEdit = Convert.ToInt32(Request.QueryString["edit"]);
                if (srtEdit > 0)
                {
                    LoadData(srtEdit);
                }
                MiscUtility.FillIndex(General_drgPriority, 30, 1);
                string[] lstDir = Directory.GetDirectories(Server.MapPath("/Layouts"));
                if (lstDir.Length > 0)
                {
                    foreach (string dir in lstDir)
                    {
                        string[] lstDirC = dir.Split('\\');
                        if (lstDirC.Length > 0)
                        {
                            string curDir = lstDirC[lstDirC.Length - 1];
                            if(!curDir.StartsWith("."))
                                General_drgLayout.Items.Add(new ListItem(curDir));                            
                        }
                    }
                }
            }
        }

        private void LoadPortal()
        {
            General_dropPortal.Items.Clear();
            List<PortalPortal> dtPortals = ManagerFactory.DAOEntity.GetList<PortalPortal>(new PortalPortal() { PORTAL_PARENTID = 0 });
            if (dtPortals != null)
            {
                PortalAccess.LoadPortals(dtPortals, General_dropPortal.Items, 0, 0);
                General_dropPortal.Items.Insert(0, new ListItem("Root", "0"));
                General_dropPortal.SelectedValue = "0";

                dgrPortals.DataSource = dtPortals;
                dgrPortals.DataBind();
            }
        }

        private void LoadData(int pId)
        {
            PortalPortal portal = ManagerFactory.DAOEntity.GetInfo(new PortalPortal() { PORTALID = pId });
            if (portal != null)
            {
                General_lblId.Text = portal.PORTALID.ToString();
                General_chkEnable.Checked = Convert.ToBoolean(portal.ENABLE);
                General_chkVisible.Checked = Convert.ToBoolean(portal.VISIBLE);
                General_FilePicker1.Text = portal.IMAGEURL;
                General_dropPortal.SelectedValue = portal.PORTAL_PARENTID.ToString();
                General_txtName.Text = portal.NAME;
                General_txtDescription.Text = portal.DESCRIPTION;
                General_drgLanguage.SelectedValue = portal.LANGUAGE.ToString();
                General_chkPermision.Checked = Convert.ToBoolean(portal.SA);
                General_drgPriority.SelectedValue = portal.PRIORITY.ToString();
                General_drgLayout.SelectedValue = portal.LAYOUT;
            }
            else
                General_lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_ERROR");
        }

        protected void dgrPortals_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            int portalId = ConvertUtility.ToInt32(e.CommandArgument);
            btnMasterPage.Enabled = true;
            btnTheme.Enabled = true;
            switch (e.CommandName)
            {
                case "editportal":
                    LoadData(portalId);
                    break;
                case "deleteportal":
                    ManagerFactory.PortalPortalManager.Delete(portalId);
                    General_lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
                    LoadPortal();
                    break;
                default:
                    break;
            }
        }

        protected void dgrPortals_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item)
            {
                var btnDeleteRole = (ImageButton) e.Item.FindControl("btnDeletePortal");
                btnDeleteRole.OnClientClick = Localization.GetText("MESSAGE", "MSG_DELETE_CONFIRM");
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            PortalPortal _portalInfo = GetData();
            _portalInfo.PORTALID = ConvertUtility.ToInt32(General_lblId.Text);
            ManagerFactory.DAOEntity.Update(_portalInfo);
            General_lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
            LoadPortal();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            PortalPortal info = GetData();
            info.THEME = BaseTemplate.BaseTheme;
            ManagerFactory.DAOEntity.Insert(info);
            General_lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
            LoadPortal();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            int srtEdit = Convert.ToInt32(Request.QueryString["edit"]);
            if (srtEdit > 0)
            {
                LoadData(srtEdit);
            }
            else
            {
                General_dropPortal.SelectedIndex = -1;
                General_txtName.Text = "";
                General_txtDescription.Text = "";
                General_chkEnable.Checked = false;
                General_chkVisible.Checked = false;
                General_chkPermision.Checked = false;
                General_drgLanguage.SelectedIndex = -1;
                General_FilePicker1.Text = "";
                General_drgPriority.SelectedIndex = -1;
            }
        }

        private PortalPortal GetData()
        {
            var portalInfo = new PortalPortal();
            portalInfo.PORTAL_PARENTID = ConvertUtility.ToInt32(General_dropPortal.SelectedValue);
            portalInfo.NAME = General_txtName.Text;
            portalInfo.DESCRIPTION = General_txtDescription.Text;
            portalInfo.VISIBLE = Convert.ToInt32(General_chkVisible.Checked);
            portalInfo.ENABLE = Convert.ToInt32(General_chkEnable.Checked);
            portalInfo.SA = Convert.ToInt32(General_chkPermision.Checked);
            portalInfo.LANGUAGE = General_drgLanguage.SelectedValue;
            portalInfo.IMAGEURL = General_FilePicker1.Text;
            portalInfo.PRIORITY = ConvertUtility.ToInt32(General_drgPriority.SelectedValue);
            portalInfo.LAYOUT = General_drgLayout.SelectedValue;            
            return portalInfo;
        }

        protected void ActiveTab(object sender, EventArgs e)
        {
            var sends = sender as LinkButton;
            if (sends != null)
            {
                panelGeneral.Visible = false;
                btnGeneral.CssClass = "tab";

                panelTheme.Visible = false;
                btnTheme.CssClass = "tab";

                panelMasterPage.Visible = false;
                btnMasterPage.CssClass = "tab";

                switch (sends.CommandArgument)
                {
                    case "1":
                        panelGeneral.Visible = true;
                        btnGeneral.CssClass = "tab tabActive";
                        break;
                    case "2":
                        panelTheme.Visible = true;
                        btnTheme.CssClass = "tab tabActive";
                        break;
                    case "3":
                        panelMasterPage.Visible = true;
                        btnMasterPage.CssClass = "tab tabActive";
                        break;
                    default:
                        break;
                }
            }
        }
    }
}